/**
 * Esta classe e responsavel pela inteligencia do player
 */
 
import core.ai.*;
import core.gameelement.agent.*;
import core.*;
import core.gameelement.*;
import core.gameelement.world.*;
import core

import util.*;
import flash.geom.*;

import rocknrollracing.gameelement.agent.*;
import rocknrollracing.gameelement.*;
 
class rocknrollracing.gameelement.agent.airocknrollracing.PlayerIntelligence extends Intelligence 
{
	private var timeAceleration:Number = 10;
	private var rotation:Number = 0; 
	private var ago:Number = 0;
	
	public function PlayerIntelligence()
	{
		super();
		Key.addListener(this);
		RocknRollRacingAgentModel(owner.getModel()).setAceleration(-30);
		
	}
	
	public function updateLogic():Void
	{	
		
		if (RocknRollRacingAgentModel(owner.getModel()).getAceleration() > 0)
  			RocknRollRacingAgentModel(owner.getModel()).addAceleration(-5);
  		
  		if (ago > 0)
  			ago += -1;
  			
  		if (rotation < 0)	
			rotation += 5;	
		
		if (rotation > 0)	
			rotation += -5;	
			
		// esta precionando o "w", para acelerar
		if (Key.isDown(87))
		{
			RocknRollRacingAgentModel(owner.getModel()).addAceleration(5);
			if (RocknRollRacingAgentModel(owner.getModel()).getAceleration() <= 40)
				RocknRollRacingAgentModel(owner.getModel()).addAceleration(10);
		}
		
		// precionando o "s" para freiar e andar para trás
  		if (Key.isDown(83))
  		{  
  			if (RocknRollRacingAgentModel(owner.getModel()).getAceleration() > 0)
  			{
  				RocknRollRacingAgentModel(owner.getModel()).addAceleration(-5);
  			}
  			else
  			{
  				if (ago < 10)
  					ago += 3;		
  			}				
  		}
  		 
		if (Key.isDown(Key.LEFT))
		{
			if (rotation > -30)	
				rotation +=- 10;
			
			var aceleration:Number = RocknRollRacingAgentModel(owner.getModel()).getAceleration();
			if (aceleration > 0 || ago > 0)
			{
				var rotation:Number = -5 * (aceleration * 0.1);
				if (aceleration == 0)
					rotation= -5 * (ago * 0.1);
					
				RocknRollRacingAgentModel(owner.getModel()).addRotation(rotation);
			}	
		}
			
		if (Key.isDown(Key.RIGHT))
		{
  			if (rotation < 30)		 
				rotation += 10;
						
			var aceleration:Number = RocknRollRacingAgentModel(owner.getModel()).getAceleration();
			
			if (aceleration > 0 || ago > 0)
			{
				var rotation:Number = 5 * (aceleration * 0.1);
				if (aceleration == 0)
					rotation = 5 * (ago * 0.1);
								
				RocknRollRacingAgentModel(owner.getModel()).addRotation(rotation);
			}
		}
		
		
		var car:MovieClip = RocknRollRacingAgentModel(owner.getModel()).getGraphic();				
		var speed:Number = RocknRollRacingAgentModel(owner.getModel()).getAceleration();
		car["mTireLeft"]._rotation = rotation;
		car["mTireRight"]._rotation = rotation;
		
		if (speed > 0)
		{			
			var targetRotation:Point = calculateAngle(speed, "front");
			owner.getModel().addPosition(targetRotation);
				
		}
		else
		{
			if (ago > 0)
			{
				var targetRotation:Point = calculateAngle(ago, "back");
				owner.getModel().addPosition(targetRotation);
				car["mTireLeft"]._rotation = rotation * -1;
				car["mTireRight"]._rotation = rotation * -1;
			}
		}		
	}
	
	public function calculateAngle(speed:Number, moviment:String):Point
	{
		var targetAngle:Point = new Point();
		var graphic:MovieClip = GameElementModel(owner.getModel()).getGraphic();
		
		if (moviment == "back")
		{
			targetAngle.x -= (Math.cos(graphic._rotation * Math.PI/180)) * speed;  
			targetAngle.y -= (Math.sin(graphic._rotation * Math.PI/180)) * speed;
		}
		else if (moviment == "front")
		{
			targetAngle.x += (Math.cos(graphic._rotation * Math.PI/180)) * speed;  
			targetAngle.y += (Math.sin(graphic._rotation  * Math.PI/180)) * speed;
		}
		
		return targetAngle;		
	}	
}